package com.example.demo.controller;

import com.example.demo.bean.User;
import com.example.demo.bean.UserInfo;
import com.example.demo.dao.UserMapper;
import com.example.demo.service.HttpUpload;
import com.example.demo.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@CrossOrigin(allowCredentials = "true",allowedHeaders = "*")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MailService mailService;
    @Autowired
    private HttpUpload httpUpload;
    /*
    * 检测用户名存在
    * */
    @RequestMapping("/checkusername")
    public String checkusername(String username){
        User user=userMapper.checkusername(username);
        if(user!=null){
            return "用户名不通过审核,用户名已存在!";
        }else {
            return "用户名通过审核!";
        }
    }
    /*
    * 根据用户名查用户
    * */
    @RequestMapping("/queryuserbyname")
    public User queryUserByname(String username){
        User user=userMapper.checkusername(username);
        return user;

    }
    /*
    * 一个邮箱最多注册三个账号,返回前端数量，由前端判断
    * */
    @RequestMapping("/countmail")
    public int countmail(String mail){
        int i=userMapper.querybymail(mail);
       return i;
    }
/*
*发送邮件信息
 */
    @RequestMapping("/sendmail")
    public String sendMail(String mail, HttpSession session){
        if(mailService.sendMimeMail(mail,session)){
            return "发送成功";
        }else {
            return "发送失败!";
        }
    }
    /*
    * 注册业务
    * */
   @RequestMapping("/register")
    public String register(@RequestBody UserInfo userInfo, HttpSession session){
       if(mailService.CheckCode(userInfo,session)){
           User user=userInfo.toUser();
           user.setImgurl("http://localhost:9090/images/default.jpg");//默认头像
           userMapper.register(user);
           return "注册成功!";
       }else {
           return "注册失败!";
       }
   }
   /*
   * 账号密码登录逻辑
   * */
    @RequestMapping("/loginbypw")
    public String loginbypw(@RequestBody User user){
        int i= userMapper.loginbypw(user);
        if(i>0){
            return "登录成功!";
        }else {
            return "登录失败!";
        }
    }
    /*
     * 验证邮箱登录逻辑
     * */
    @RequestMapping("/loginbymail")
    public String loginbymail(@RequestBody UserInfo userInfo,HttpSession session){
        int i=userMapper.querybymail(userInfo.getMail());
        if(i>0){
            if(mailService.CheckCode(userInfo,session)){
                return "登录成功!";
            }else {
                return "验证码错误!";
            }
        }else {
            return "邮箱不存在!";
        }
    }
    /*
    * 用户图片上传
    * */
    @RequestMapping("/upload")
    public String upload(@RequestParam("pic") List<MultipartFile> uploadFile){
        String str = httpUpload.uppic(uploadFile);
        if (!str.equals("false")) {
            return str;  //最后返回访问的url
        } else {
            return "上传失败!";
        }
    }
    /*
    * 用户头像修改
    * */
    @RequestMapping("/updateurl")
    public String updateurl(@RequestBody User user){
        int i=userMapper.updateurl(user);
        if(i>0){
            return "修改成功!";
        }else {
            return "修改失败!";
        }
    }
}
